package mephisto.rest;

import mephisto.rest.resources.BinaryResource;
import mephisto.rest.resources.PageResource;

import org.restlet.Component;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.Protocol;
import org.restlet.ext.wadl.WadlApplication;

public class RestApplication extends WadlApplication {

    RestApplication() {
        super();
    }

    public RestApplication(Context parentContext) {
        super(parentContext);
    }

    /**
     * Creates a root Restlet that will receive all incoming calls.
     */
    @Override
    public synchronized Restlet createRoot() {
        // Create a router Restlet that routes each call to a
        // new instance of HelloWorldResource.
        Router router = new Router(getContext());

        getTunnelService().setEnabled(true);
        /* We cannot activate the extension tunnel service because otherwise attachments will not be correctly handled */
        getTunnelService().setExtensionsTunnel(false);

        
        // Defines only one route
        router.attachDefault(PageResource.class);

        router.attach("/mephisto/{key}", PageResource.class);
        //persons/{namespaceid}/{id}/annotations
        //applications/{namespaceid}/{id}/annotations
        //namespaceid/resourceid/wikimodel -> return wikimodel rendered view of the corresponding id
        //namespaceid/resourceid/groovy ->
        //namespaceid/resourceid put -> store page
        //discussions
        //discussions/namespaceid/id/annotations
        // router.attach("/version/", ApplicationVersionProvider.class);
        // router.attach("/namespace/id/wikimodel", WikiModelResource.class);
        // router.attach("/storage", FileStorageService.class);
        // router.attach("/storage/{content-id}", FileStorageService.class);
        router.attach("/mephisto/images/{content-id}", BinaryResource.class);

        // groovyRoute.getTemplate().getVariables().put("URI", new
        // Variable(Variable.TYPE_URI_ALL));

        return router;
    }

    public static void main(String[] args) {

        try {

            // Create a new Component.
            Component component = new Component();

            // Add a new HTTP server listening on port 8182.
            component.getServers().add(Protocol.HTTP, 8182);

            // Attach the sample application.
            component.getDefaultHost().attach(new RestApplication());

            // Start the component.
            component.start();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
